﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class ChiTietPhieuChiDAO
    {
        private static QUANLYTIEMVANGEntities dbContext = new QUANLYTIEMVANGEntities();

        public static void Insert(CHITIETPHIEUCHI ctpc)
        {
            dbContext.CHITIETPHIEUCHIs.Add(ctpc);
            dbContext.SaveChanges();
        }
        public static void Delete(CHITIETPHIEUCHI ctpc)
        {
            if (dbContext.CHITIETPHIEUCHIs.Find(ctpc.MaChiTietPhieuChi) == null)
                return;

            dbContext.CHITIETPHIEUCHIs.Remove(ctpc);
            dbContext.SaveChanges();
        }

        public static void Update(CHITIETPHIEUCHI ctpc)
        {
            var query = dbContext.CHITIETPHIEUCHIs.Find(ctpc.MaChiTietPhieuChi);
            if (query != null)
            {
                query.MaKhoanPhi = ctpc.MaKhoanPhi;
                query.MaPhieuChi = ctpc.MaPhieuChi;
                query.ThanhTien = ctpc.ThanhTien;
                dbContext.SaveChanges();                
            }
        }
        //news Tam
        public static KHOANPHI getKhoanPhi(CHITIETPHIEUCHI temp)
        {
            var query = from kp in dbContext.KHOANPHIs
                        where kp.MaKhoanPhi == temp.MaKhoanPhi
                        select kp;
            if (query.Count() > 0)
            {
                return query.First();
            }
            return null;
        }

        public static List<CHITIETPHIEUCHI> GetListChiTietPhieuChi(int maPhieuChi)
        {
            var query = from d in dbContext.CHITIETPHIEUCHIs
                        where d.MaPhieuChi == maPhieuChi
                        select d;
            return query.ToList();
        }
    }
}
